package com.fxp.project.jiuselu.web.models.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.fxp.project.jiuselu.web.models.user.entity.JslUserAddress;
import com.fxp.project.jiuselu.web.models.user.mapper.JslUserAddressMapper;
import com.fxp.project.jiuselu.web.models.user.service.IJslUserAddressService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * <p>
 * 用户地址表 服务实现类
 * </p>
 *
 * @author Jason.Wang
 * @since 2021-11-10
 */
@Service
public class JslUserAddressServiceImpl extends ServiceImpl<JslUserAddressMapper, JslUserAddress> implements IJslUserAddressService {

    @Autowired
    JslUserAddressMapper jslUserAddressMapper;

    /**
     * selectJslUserAddressListByUserId: 获取当前用户的地址列表信息<br/>
     *
     * @param userId
     * @return
     * @author Kai.Wang
     * @date 2021/11/10 13:37
     */
    @Override
    public List<JslUserAddress> selectJslUserAddressListByUserId(Long userId) {
        return jslUserAddressMapper.selectJslUserAddressListByUserId(userId);
    }

    /**
     * selectDefaultJslUserAddressByUserIdWithAddressId: 根据用户的Id获取当前的地址信息<br/>
     *
     * @param userId
     * @return
     * @author Kai.Wang
     * @date 2021/11/10 13:43
     */
    @Override
    public JslUserAddress selectDefaultJslUserAddressByUserIdWithAddressId(Long userId) {
        return jslUserAddressMapper.selectDefaultJslUserAddressByUserIdWithAddressId(userId);
    }

    /**
     * selectJslUserAddressByUserIdWithAddressId: 根据当前用户获取指定的地址信息<br/>
     *
     * @param userId
     * @param addressId
     * @return
     * @author Kai.Wang
     * @date 2021/11/10 13:42
     */
    @Override
    public JslUserAddress selectJslUserAddressByUserIdWithAddressId(Long userId, Long addressId) {
        return jslUserAddressMapper.selectJslUserAddressByUserIdWithAddressId(userId, addressId);
    }

    /**
     * updateUserAddressToDefault: 设置当前地址为默认地址，并且有且只有一个默认地址<br/>
     *
     * @param addressId
     * @param userId
     * @return
     * @author Kai.Wang
     * @date 2021/11/10 15:19
     */
    @Override
    @Transactional
    public int updateUserAddressToDefault(Long addressId, Long userId) {
        JslUserAddress param = new JslUserAddress();
        param.setIsDefault(0);
        QueryWrapper<JslUserAddress> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", userId);
        jslUserAddressMapper.update(param, queryWrapper);
        param.setAddressId(addressId);
        param.setIsDefault(1);
        return jslUserAddressMapper.updateById(param);
    }
}
